#include "editorexitdialog.h"

EditorExitDialog::EditorExitDialog(QString tabName, QWidget *parent) :
    QDialog(parent)
{
    exitDialogButtons_ = new QDialogButtonBox(QDialogButtonBox::Save | QDialogButtonBox::Cancel | QDialogButtonBox::Discard);
    exitDialogLayout_ = new QVBoxLayout(this);
    exitDialogLabel_ = new QLabel("The file " + tabName + " has unsaved changes. Do you want to exit anyway?");

    exitDialogLayout_->addWidget(exitDialogLabel_);
    exitDialogLayout_->addWidget(exitDialogButtons_);

    connect(exitDialogButtons_, SIGNAL(clicked(QAbstractButton*)), this, SLOT(onClicked(QAbstractButton*)));
}

EditorExitDialog::~EditorExitDialog()
{
    delete exitDialogLabel_;
    delete exitDialogButtons_;
    delete exitDialogLayout_;
}

EditorExitDialog::ExitCode EditorExitDialog::exec()
{
    int exit_code = QDialog::exec();

    if(exit_code == Save)
        return Save;
    else if(exit_code == Cancel)
        return Cancel;
    else
        return Discard;
}

void EditorExitDialog::onClicked(QAbstractButton *button)
{
    if(exitDialogButtons_->buttonRole(button) == QDialogButtonBox::AcceptRole)
        QDialog::done(Save);
    else if(exitDialogButtons_->buttonRole(button) == QDialogButtonBox::RejectRole)
        QDialog::done(Cancel);
    else if(exitDialogButtons_->buttonRole(button) == QDialogButtonBox::DestructiveRole)
        QDialog::done(Discard);
}
